package com.weme.chat.ext

import android.util.Log
import androidx.lifecycle.lifecycleScope
import com.weme.chat.ChatActivity
import com.weme.common.util.showToast
import com.weme.common.util.smoothScrollToBottom
import dev.kratos.api.im_server.v1.AfterSendImMsgRequest
import io.agora.CallBack
import io.agora.chat.ChatClient
import io.agora.chat.ChatMessage
import io.agora.chat.TextMessageBody
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch


object ChatExt {}



fun ChatActivity.addMessage(msg: ChatMessage){
    lifecycleScope.launch(Dispatchers.Main) {
      //  conversation.insertMessage(msg)
      //  ChatClient.getInstance().chatManager().updateMessage(msg)

        chatList.add(msg)
        chatAdapter.notifyDataSetChanged()
        alertDialog?.dismiss()
        getViewBinding().rcvList.smoothScrollToBottom()

        if(getViewMode().conversation == null){
            onResume()
        }
    }
}
fun ChatActivity.sendMseeage(message: ChatMessage){


    message.setMessageStatusCallback(object : CallBack {
        override fun onSuccess() {

            Log.e("tag", "Send message success!") //    showToast("Send message success!")
            addMessage(message)
            when(message.type ){
                ChatMessage.Type.TXT->{
                    getViewMode().afterSendImMsg(AfterSendImMsgRequest.newBuilder().setToUid(getViewMode().conversationId.toLong()).setContent((message.body as TextMessageBody).message).setMsgType(1).build())
                }
                ChatMessage.Type.IMAGE->{
                    getViewMode().afterSendImMsg(AfterSendImMsgRequest.newBuilder().setToUid(getViewMode().conversationId.toLong()).setMsgType(2).build())
                }
                ChatMessage.Type.CUSTOM->{
                    getViewMode().afterSendImMsg(AfterSendImMsgRequest.newBuilder().setToUid(getViewMode().conversationId.toLong()).setMsgType(3).build())

                }
                else -> {

                }
            }
            // 未回复消息
//            if(message.type== ChatMessage.Type.TXT||message.type== ChatMessage.Type.IMAGE){
//                val customMessage = ChatMessage.createSendMessage(ChatMessage.Type.CUSTOM)
//                val event = AgoraClient.EVENT_TYPING_NO_RESPONSE
//                val customBody = CustomMessageBody(event)
//                val systemMessage = SystemMessage("1")
//                val systeMessagemMap = systemMessage.toMap()
//                customBody.params = systeMessagemMap
//                customMessage.addBody(customBody)
//                customMessage.to = conversationId
//                customMessage.chatType = ChatMessage.ChatType.Chat
//                ChatClient.getInstance().chatManager().sendMessage(customMessage)
//                message.setMessageStatusCallback(null)
//            }

        }

        override fun onError(code: Int, error: String) {
            Log.e("tag", error)
            message.setDeliverAcked(false)
          //  addMessage(message)
            showToast(error)
            message.setMessageStatusCallback(null)
        }
    })

    // 发送消息。
    ChatClient.getInstance().chatManager().sendMessage(message)

}

data class TranslationItem(val position: Int, val content: String)

// TranslationManager.kt
object TranslationManager {
    private val queue: MutableList<TranslationItem> = mutableListOf()

    fun addToQueue(position: Int, content: String) {
        val item = TranslationItem(position, content)
        queue.add(item)
    }

    fun getAllItems(): List<TranslationItem> {
        return queue
    }

    fun removeFirstItem() {
        if (queue.isNotEmpty()) {
            queue.removeAt(0)
        }
    }

    fun getFirstItem(): TranslationItem? {
        return if (queue.isNotEmpty()) {
            queue.first()
        } else {
            null
        }
    }
}